<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Account extends Model
{
    public static function getBalance(){
        $user = session('admin_user');
        $account = Account::where("user_id",$user["id"])->value("balance");
        if(empty($account)) return 0;
        return $account;
    }
    protected $table = 'accounts';
    /**
     * 余额变化
     * @param $user_id
     * @param $io
     * @param $price
     * @param string $remark
     * @return bool
     */
    public static function money($user_id,$io,$price,$remark=""){
        $account = Account::where("user_id",$user_id)->first();
        if($io == 1) {
            $account->increment("balance",$price);
            $account->increment("total",$price);
        }else{
            $account->decrement("balance",$price);
        }
        $log = new AccountLog();
        $log->user_id = $user_id;
        $log->io = $io;
        $log->price = $price;
        $log->balance = $account->balance;
        $log->remark = $remark;
        $log->save();
        return true;
    }

}
